﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using VirtualDrum.lib;
using VirtualDrum.lib.db;

namespace VirtualDrum.componets
{
	public partial class frmDrumGroupInfo : Form
	{

		public frmDrumGroupInfo()
		{
			InitializeComponent();
			this._groupName = "";
			this._actionName = "add";
			DBServer = new DBFactory().createDBServer();
		}

		private void btnDrumGroupReturn_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		/// <summary>
		/// 执行对应的操作
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDrumGroupSure_Click(object sender, EventArgs e)
		{
			int result = 0;
			string groupName = this.tbDrumGroupName.Text.Trim();
			if(groupName.Equals("")){
				MessageBox.Show("鼓组不能为空！");
				return;
			}			
			if(this._actionName.Equals("add")){
				result = this.addGroupInfo(groupName);
			}else if(this._actionName.Equals("change")){
				result = this.changeGroupInfo(groupName);
				DBServer.closeConnection();	
				this.chageGroupMusicGroupName(groupName);
			}
			if (result > 0)
			{
				MessageBox.Show("操作成功！");
			}
			else
			{
				MessageBox.Show("操作失败！");
			}
			DBServer.closeConnection();	
		}

		/// <summary>
		/// 修改或添加鼓组信息
		/// </summary>
		/// <param name="groupName"></param>
		/// <returns>int</returns>
		private int addGroupInfo(string groupName){
			string nowTime = DateTime.Now.ToString();
			string sql = string.Format("INSERT INTO [vd_drumgroup] ([groupName],[groupMusicPath],[createTime]) VALUES ('{0}','{1}','{2}');", groupName, this.lbFilePath.Text,DBServer.getDateTime());
			return DBServer.executeNonQuery(sql);
			
		}

		/// <summary>
		/// 修改鼓组信息
		/// </summary>
		/// <param name="groupName"></param>
		/// <returns>int</returns>
		private int changeGroupInfo(string groupName)
		{
			string sql = string.Format("UPDATE [vd_drumgroup] SET [groupName] = '{0}',[groupMusicPath]='{1}',[createTime] = '{2}' WHERE [groupName] = '{3}';", groupName, this.lbFilePath.Text,DBServer.getDateTime(),this._groupName);
			return DBServer.executeNonQuery(sql);
		}

		private int chageGroupMusicGroupName(string groupName){
			string sql = string.Format("UPDATE [vd_drummusic] SET [musicGroupName] = '{0}' WHERE [musicGroupName] = '{1}';", groupName,this._groupName);
			return DBServer.executeNonQuery(sql);
		}

		/// <summary>
		/// 设置当前的操作类型
		/// </summary>
		public string ActionName{
			get { return this._actionName; }
			set { this._actionName = value; }
		}

		/// <summary>
		/// 当前操作ID的Settor Getter
		/// </summary>
		public string GroupName
		{
			get { return this._groupName; }
			set { this._groupName = value; }
		}

		public string TbDrumGroupName{
			get { return this.tbDrumGroupName.Text; }
			set { this.tbDrumGroupName.Text = value; }
		}

		private void frmDrumGroupInfo_Load(object sender, EventArgs e)
		{
			this.tbDrumGroupName.Text = this._groupName;
		}

		public string GroupMusicPath{
			set { this.lbFilePath.Text = value; }
		}

		/// <summary>
		/// 当前操作鼓组ID
		/// </summary>
		private string _groupName;		
		/// <summary>
		/// 当前的操作类型
		/// </summary>
		private string _actionName;

		private static ControlDatabase DBServer;

		private void btnChoiceMusicFile_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK == this.ofdDrumGroupMusic.ShowDialog())
			{
				this.lbFilePath.Text = this.ofdDrumGroupMusic.FileName;
			}
		}

		/// <summary>
		/// 清除音源路径事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void lbFilePath_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK == MessageBox.Show("确定清除文件？", frmMain.projectName, MessageBoxButtons.OKCancel))
			{
				this.lbFilePath.Text = "";
			}
		}

	}
}
